/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.notaria.guerrasalas.bc;

import gob.satt.comun.jpa.Command;
import java.util.Vector;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import oracle.toplink.essentials.config.HintValues;
import oracle.toplink.essentials.config.TopLinkQueryHints;

/**
 *
 * @author Administrator
 */
public class ValidarUtilizacionKardexCommand implements Command<Boolean> {

    private Long idKardex;

    public ValidarUtilizacionKardexCommand(Long idKardex) {
        this.idKardex = idKardex;
    }

    @Override
    public Boolean execute(EntityManager em) throws Exception {
        Boolean rpta = true;
        Long idActoJuridico = null;
        try {
            idActoJuridico = (Long) ((Vector) em.createNativeQuery("SELECT ID_ACTO_JURIDICO FROM ACTO_JURIDICO WHERE FID_KARDEX = ?1 AND ESTADO = 0")
                    .setParameter(1, idKardex)
                    .setHint(TopLinkQueryHints.REFRESH, HintValues.TRUE)
                    .getSingleResult()).get(0);
        } catch (NoResultException e) {
            rpta = false;
        } catch (NonUniqueResultException e) {
            rpta = true;
        } catch (IllegalStateException e) {
            rpta = true;
        }
        return rpta;
    }
}
